package com.example.userp.app.myapplication.RcycleView.flexibledivider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.DimenRes;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * Describe: 横向分割线
 * Created by zdh on 2018-09-06.
 */
public class HorizontalDividerItemDecoration extends FlexibleDividerDecoration {

    private MarginProvider mMarginProvider;
    private Builder builder;

    protected HorizontalDividerItemDecoration(Builder builder) {
        super(builder);
        this.builder = builder;

        mMarginProvider = builder.mMarginProvider;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();
        int childAdapterPosition = parent.getChildAdapterPosition(view);
        int dividerSize = getDividerSize(childAdapterPosition, parent);
        if (childAdapterPosition == 0 && mShowFirstDivider) {//限制第一个
            outRect.top = getDividerSize(childAdapterPosition, parent);
        }

        if (itemCount - 1 > childAdapterPosition) {//判断是否是最后一个
            outRect.bottom = dividerSize;
        } else {
            if (mShowLastDivider) {//限制最后一个
                outRect.bottom = dividerSize;
            }
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        setDividerType();
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();

        int childCount = parent.getChildCount();
        int height = parent.getHeight();
        int width = parent.getWidth();

        for (int i = 0; i < childCount; i++) {
            View childAt = parent.getChildAt(i);
            int childAdapterPosition = parent.getChildAdapterPosition(childAt);
            int dividerSize = getDividerSize(childAdapterPosition, parent);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childAt.getLayoutParams();
            int left = parent.getPaddingLeft() + layoutParams.leftMargin + mMarginProvider.dividerLeftMargin(childAdapterPosition, parent);
            int top = childAt.getTop() - dividerSize;
            int right = width - parent.getPaddingRight() - layoutParams.rightMargin - mMarginProvider.dividerRightMargin(childAdapterPosition, parent);
            int bottom = childAt.getTop();
            if (top >= parent.getPaddingTop() - dividerSize && bottom <= height - parent.getPaddingBottom()) {
                if (childAdapterPosition > 0) {
                    c.drawRect(left, top, right, bottom, mPaint);
                } else {
                    if (mShowFirstDivider) {
                        c.drawRect(left, top, right, bottom, mPaint);
                    }
                }
                if (childAdapterPosition == itemCount - 1 && mShowLastDivider) {
                    c.drawRect(left, childAt.getBottom(), right, childAt.getBottom() + dividerSize, mPaint);
                }
            }
        }

    }

    /**
     * Interface for controlling divider margin
     */
    public interface MarginProvider {

        /**
         * Returns left margin of divider.
         *
         * @param position Divider position (or group index for GridLayoutManager)
         * @param parent   RecyclerView
         * @return left margin
         */
        int dividerLeftMargin(int position, RecyclerView parent);

        /**
         * Returns right margin of divider.
         *
         * @param position Divider position (or group index for GridLayoutManager)
         * @param parent   RecyclerView
         * @return right margin
         */
        int dividerRightMargin(int position, RecyclerView parent);
    }

    public static class Builder extends FlexibleDividerDecoration.Builder<Builder> {

        private MarginProvider mMarginProvider = new MarginProvider() {
            @Override
            public int dividerLeftMargin(int position, RecyclerView parent) {
                return 0;
            }

            @Override
            public int dividerRightMargin(int position, RecyclerView parent) {
                return 0;
            }
        };

        public Builder(Context context) {
            super(context);
        }

        public Builder margin(final int leftMargin, final int rightMargin) {
            return marginProvider(new MarginProvider() {
                @Override
                public int dividerLeftMargin(int position, RecyclerView parent) {
                    return leftMargin;
                }

                @Override
                public int dividerRightMargin(int position, RecyclerView parent) {
                    return rightMargin;
                }
            });
        }

        public Builder margin(int horizontalMargin) {
            return margin(horizontalMargin, horizontalMargin);
        }

        public Builder marginResId(@DimenRes int leftMarginId, @DimenRes int rightMarginId) {
            return margin(mResources.getDimensionPixelSize(leftMarginId),
                    mResources.getDimensionPixelSize(rightMarginId));
        }

        public Builder marginResId(@DimenRes int horizontalMarginId) {
            return marginResId(horizontalMarginId, horizontalMarginId);
        }

        public Builder marginProvider(MarginProvider provider) {
            mMarginProvider = provider;
            return this;
        }

        public HorizontalDividerItemDecoration build() {

            checkBuilderParams();
            return new HorizontalDividerItemDecoration(this);
        }
    }

    public void setDividerType(){
        switch (mDividerType) {

            case COLOR:
                mPaint.setColor(mColorProvider.dividerColor(0, null));
                mPaint.setStrokeWidth(mSizeProvider.dividerSize(0, null));
                break;
        }
    }
}